﻿using DbUpgradeOracle.General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DbUpgradeOracle
{
    public partial class FormAddDbInfo : Form
    {
        private bool _isAdd = false;
        private int _id;
        private DbInfo _dbInfo = null;
        public FormAddDbInfo()
        {
            InitializeComponent();
        }

        public static DialogResult ShowAddDbInfo()
        {
            FormAddDbInfo form = new FormAddDbInfo();
            form._isAdd = true;
            return form.ShowDialog();
        }

        public static DialogResult ShowEditDbInfo(int id)
        {
            FormAddDbInfo form = new FormAddDbInfo();
            form._isAdd = false;
            form._id = id;
            return form.ShowDialog();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string chineseName = edtChineseName.Text.Trim();
            string checkName = edtCheckName.Text.Trim();
            if (string.IsNullOrEmpty(chineseName))
            {
                MessageBox.Show("请输入库中文显示名称");
            }
            else if (string.IsNullOrEmpty(checkName))
            {
                MessageBox.Show("请输入库升级校验名称");
            }
            else
            {
                if (_isAdd)
                {
                    if (CheckAdd(chineseName, checkName))
                    {
                        string dbKey = Guid.NewGuid().ToString("N");
                        string sql = string.Format("insert into DbInfo (DbChineseName,DbKey,DbCheckName) values ('{0}','{1}','{2}')", chineseName, dbKey, checkName);
                        int i = App.DalData.ExecNoQuery(sql);
                        if (i > 0)
                        {
                            MessageBox.Show("新增成功");
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            MessageBox.Show("新增失败");
                        }
                    }
                    else
                    {
                        MessageBox.Show("名称重复，无法添加");
                    }
                }
                else
                {
                    string sql = $"update DbInfo set DbChineseName='{chineseName}' where id={_id}";
                    int i = App.DalData.ExecNoQuery(sql);
                    if (i > 0)
                    {
                        MessageBox.Show("修改成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
            }
        }

        private void FormAddDbInfo_Load(object sender, EventArgs e)
        {
            if (!_isAdd)
            {
                edtCheckName.Enabled = false;
                RefreshData();
            }
        }

        private void RefreshData()
        {
            Task.Run(() =>
            {
                string sql = $"select * from DbInfo where id = {_id}";
                DataTable dt = App.DalData.ExecDataTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    DbInfo ver = new DbInfo();
                    ver.Id = dr["Id"].ToInt32Ex();
                    ver.DbChineseName = dr["DbChineseName"].ToStringEx();
                    ver.DbKey = dr["DbKey"].ToStringEx();
                    ver.DbCheckName = dr["DbCheckName"].ToStringEx();
                    _dbInfo = ver;
                    this.BeginInvoke(new Action(() =>
                    {
                        edtChineseName.Text = _dbInfo.DbChineseName;
                        edtCheckName.Text = _dbInfo.DbCheckName;
                    }));
                }
            });
        }

        /// <summary>
        /// 判断是否可以添加
        /// </summary>
        /// <param name="chineseName"></param>
        /// <param name="checkName"></param>
        /// <returns></returns>
        private bool CheckAdd(string chineseName,string checkName)
        {
            string sql = $"select * from DbInfo where DbChineseName = '{chineseName}'";
            DataTable dt = App.DalData.ExecDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return false;
            }
            else
            {
                string sql2 = $"select * from DbInfo where DbCheckName = '{checkName}'";
                DataTable dt2 = App.DalData.ExecDataTable(sql2);
                if (dt2 != null && dt2.Rows.Count > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}
